package game;

import utils.RandomUtils;

import java.awt.*;
import java.lang.reflect.Field;

public class Catus extends Sprite{
    GameManager game = GameManager.getInstance();
    @Override
    public void init() {
        super.x = 600;
        super.y = 100;
        String type = "small";
        int typeInt = RandomUtils.randomInt(0,10);
        if (typeInt%3 == 0){
            type = "big";
            super.y = 90;
        }//两种类型的图片
        int numInt = RandomUtils.randomInt(1,3);//图片大小
        super.image = ImageManager.loadImage("src/Images/"+ GameData.Skin +"/cactus_"+type+"_"+numInt+".png");
        super.width = super.image.getWidth();
        super.height = super.image.getHeight();
        super.speed = 4;
    }
    private boolean flag = false;

    @Override
    public void move() {
        super.x = super.x -super.speed;
        if (super.x+super.width<90 && get_jump()){
            flag=true;
        }
        if(x<=-50){
            this.destroy();
        }
    }
    public Rectangle getBounds(){
        return new Rectangle(x+8,y+10,width-8,height-10);
    }//返回矩形大小
    @Override
    public void destroy() {
        add_jump_catus(flag);
        game.catusList.remove(this);
    }

    private Boolean get_jump() {
        boolean jump = false;
        try {
            Trex trex = game.trex;
            Field field;
            field = Trex.class.getDeclaredField("jump");
            field.setAccessible(true);
            jump = (boolean) field.get(trex);
            field.setAccessible(false);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jump;
    }//反射获取jump参数
    private void add_jump_catus(boolean flag){
        if (flag){
            GameData.jump_catus++;
        }
    }
}
